package com.wgchao.amc.controller.autho;

import com.wgchao.amc.common.BindingError;
import com.wgchao.amc.common.MenuAutho;
import com.wgchao.amc.common.ResultCommon;
import com.wgchao.amc.common.validate.Save;
import com.wgchao.amc.mapstruct.dto.SysAuthoRoleDTO;
import com.wgchao.amc.service.RoleAuthoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName: RoleAuthoController
 * @author: wgchao
 * @createTime: 2019/10/31 10:07 AM
 * @Description: 部门角色权限管理
 */
@RestController
@RequestMapping("/amc/sys/autho/role")
@Api(tags = "部门角色菜单权限管理")
public class RoleAuthoController {
    @Autowired
    private RoleAuthoService roleAuthoService;

    @PostMapping("/assign/{groupId}/{roleId}/{proMark}")
    @ApiOperation(value = "为部门角色分配菜单权限")
    @MenuAutho(desc = "为部门角色分配菜单权限")
    @ApiParam(name = "authoRoleDTOS", value = "菜单权限信息", required = true)
    public ResultCommon<List<SysAuthoRoleDTO>> assign(@PathVariable Long groupId,
                                                      @PathVariable Long roleId,
                                                      @PathVariable String proMark,
                                                      @RequestBody @Validated(value = {Save.class}) List<SysAuthoRoleDTO> authoRoleDTOS,
                                                      BindingResult bindingResult) throws Exception{
        String error = null;
        if((error = BindingError.error(bindingResult)) != null){
            return ResultCommon.fail(error);
        }
        return roleAuthoService.assign(groupId, roleId, proMark, authoRoleDTOS);
    }

    @GetMapping("/search/{groupId}/{roleId}/{proMark}")
    @ApiOperation(value = "查询部门角色菜单权限")
    @MenuAutho(desc = "查询部门角色菜单权限")
    public ResultCommon<List<SysAuthoRoleDTO>> search(@PathVariable Long groupId,
                                                      @PathVariable Long roleId,
                                                       @PathVariable String proMark) throws Exception{
        return roleAuthoService.search(groupId, roleId, proMark);
    }
}
